package com.flex.mobile.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalenderUtil {

   private static String split_pattern = "yyyy-MM-dd HH:mm:ss";

   private static String currentDate_pattern = "yyyyMMdd";

   private static String time_pattern = "yyyyMMddHHmmss";

   public static String getFormatedTimeString(long timeLong, String pattern) {
      SimpleDateFormat sdf = new SimpleDateFormat();
      sdf.applyPattern(pattern);
      return sdf.format(new Date(timeLong));
   }

   public static String getCurrentTimeString() {
      return getTimeString(System.currentTimeMillis());
   }

   public static String getCurrentDateString() {
      Date dt = new Date();
      return getFormatedTimeString(dt.getTime(), currentDate_pattern);
   }

   public static String getTimeString(long timeLong) {
      return getFormatedTimeString(timeLong, time_pattern);
   }

   public static String getSpitTimeString(long timeLong) {
      return getFormatedTimeString(timeLong, split_pattern);
   }

   public static long getCurrentTime() {
      return (new Date()).getTime();
   }

   public static long getTime(String timeString) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat();
      sdf.applyPattern("yyyyMMddHHmmss");
      Date dt = sdf.parse(timeString);
      return dt.getTime();
   }

   public static String getTimeDescription(long time) {
      SimpleDateFormat sdf = new SimpleDateFormat();
      sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
      return sdf.format(new Date(time));
   }

   // //////////////////////////////////////////////

   public static Date addMillisecond(Date dt, int millisecond) {
      return addSecond(dt, (long) millisecond);
   }

   public static Date addMillisecond(Date dt, long millisecond) {
      dt.setTime(dt.getTime() + millisecond);
      return dt;
   }

   // add second
   public static Date addSecond(Date dt, int second) {
      return addSecond(dt, (long) second);
   }

   public static Date addSecond(Date dt, float second) {
      return addSecond(dt, (double) second);
   }

   public static Date addSecond(Date dt, long second) {
      return addMillisecond(dt, 1000L * second);
   }

   public static Date addSecond(Date dt, double second) {
      Double millisecond = new Double(1000.0 * second);
      return addMillisecond(dt, millisecond.longValue());
   }

   // add minute
   public static Date addMinute(Date dt, int minute) {
      return addMinute(dt, (long) minute);
   }

   public static Date addMinute(Date dt, float minute) {
      return addMinute(dt, (double) minute);
   }

   public static Date addMinute(Date dt, long minute) {
      return addMillisecond(dt, 1000L * 60L * minute);
   }

   public static Date addMinute(Date dt, double minute) {
      Double millisecond = new Double(1000.0 * 60.0 * minute);
      return addMillisecond(dt, millisecond.longValue());
   }

   // add hour
   public static Date addHour(Date dt, int hour) {
      return addHour(dt, (long) hour);
   }

   public static Date addHour(Date dt, float hour) {
      return addHour(dt, (double) hour);
   }

   public static Date addHour(Date dt, long hour) {
      return addMillisecond(dt, 1000L * 60L * 60L * hour);
   }

   public static Date addHour(Date dt, double hour) {
      Double millisecond = new Double(1000.0 * 60.0 * 60.0 * hour);
      return addMillisecond(dt, millisecond.longValue());
   }

   // add day
   public static Date addDay(Date dt, int day) {
      return addDay(dt, (long) day);
   }

   public static Date addDay(Date dt, float day) {
      return addDay(dt, (double) day);
   }

   public static Date addDay(Date dt, long day) {
      return addMillisecond(dt, 1000L * 60L * 60L * 24L * day);
   }

   public static Date addDay(Date dt, double day) {
      Double millisecond = new Double(1000.0 * 60.0 * 60.0 * 24.0 * day);
      return addMillisecond(dt, millisecond.longValue());
   }

   // add month
   public static Date addMonth(Date dt, int month) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(dt);
      cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
      return cal.getTime();
   }

   public static Date addMonth(Date dt, float month) {
      return addMonth(dt, (double) month);
   }

   public static Date addMonth(Date dt, long month) {
      return addMonth(dt, (new Long(month)).intValue());
   }

   public static Date addMonth(Date dt, double month) {
      double floorMonth = Math.floor(month);
      double decimalMonth = month - floorMonth;
      dt = addMonth(dt, (new Double(floorMonth)).intValue());
      Calendar cal = Calendar.getInstance();
      cal.setTime(dt);
      cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
      Date nextdt = cal.getTime();
      long monthMillisecond = nextdt.getTime() - dt.getTime();
      double millisecond = (double) monthMillisecond * decimalMonth;
      return addMillisecond(dt, (long) millisecond);
   }

   // add year
   public static Date addYear(Date dt, int year) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(dt);
      cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + year);
      return cal.getTime();
   }

   public static Date addYear(Date dt, float year) {
      return addYear(dt, (double) year);
   }

   public static Date addYear(Date dt, long year) {
      return addYear(dt, (new Long(year)).intValue());
   }

   public static Date addYear(Date dt, double year) {
      double floorYear = Math.floor(year);
      double decimalYear = year - floorYear;
      dt = addYear(dt, (new Double(floorYear)).intValue());
      Calendar cal = Calendar.getInstance();
      cal.setTime(dt);
      cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
      Date nextdt = cal.getTime();
      long yearMillisecond = nextdt.getTime() - dt.getTime();
      double millisecond = (double) yearMillisecond * decimalYear;
      return addSecond(dt, (long) millisecond);
   }

   public static long getPeriodLong(int days, int hour, int minutes) {
      return days * 24 * 60 * 60 * 1000L + hour * 60 * 60 * 1000L + minutes
            * 60 * 1000L;
   }

   public static long getDays(long times) {
      return times / (24 * 60 * 60 * 1000L);
   }

   public static long getHours(long times) {
      return times / (60 * 60 * 1000L);
   }

   public static long getMinutes(long times) {
      return times / (60 * 1000L);
   }

   public static String getDHM(long times) {
      long hours = times % (24 * 60 * 60 * 1000L);
      long minutes = hours % ((60 * 60 * 1000L));
      return String.valueOf(getDays(times)) + "�� "
            + String.valueOf(getHours(hours)) + "Сʱ: "
            + String.valueOf(getMinutes(minutes)) + "����";
   }

   public static int getHour24OfTime(long time) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(time));
      return c.get(Calendar.HOUR_OF_DAY);
   }

   public static int getMiniteOfTime(long time) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(time));
      return c.get(Calendar.MINUTE);
   }

   public static int getSecondOfTime(long time) {
      Calendar c = Calendar.getInstance();
      c.setTime(new Date(time));
      return c.get(Calendar.SECOND);
   }

   /**
    * @param id
    * @param ih
    * @param im
    * @return
    */
   public static long getTimeLongFromDHM(int id, int ih, int im) {
      return ((long) id) * 24 * 60 * 60 * 1000 + ((long) ih) * 60 * 60 * 1000
            + ((long) im) * 60 * 1000;
   }
}